package org.project.product.android.pfe_fanta;

import org.project.product.android.pfe.fanta.R;
import org.project.product.android.pfe.fanta.adapter.DataManagerFacade;
import org.project.product.android.pfe.fanta.domain.User;
import org.project.product.android.pfe.fanta.util.Utils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Main extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (!DataManagerFacade.getInstance(this).haveAlreadyUserAccount()) {
			Utils.lancerActiviter(this, UserActivity.class);
		}

		setContentView(R.layout.activity_main);

	}

	public void loginUser(View v) {
		EditText login = (EditText) findViewById(R.id.edit_login);
		EditText password = (EditText) findViewById(R.id.edit_password);
		String loginValue = login.getText().toString();
		String passwordValue = password.getText().toString();

		if (Utils.checkEditText(this, login, password)) {
			User user = new User();
			user.setLogin(loginValue);
			user.setPassword(passwordValue);
			if (DataManagerFacade.getInstance(getApplicationContext())
			        .isValidUser(user)) {
				Utils.lancerActiviter(this, Home.class);
			} else {
				Toast.makeText(this, "incorrecte value", Toast.LENGTH_LONG)
				        .show();
			}
		}
	}

	public void forgot(View v) {
		Utils.lancerActiviter(this, ForgotHelp.class);
	}

	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}

}
